<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title>编辑指令</title>
	<#include "/common/resource.ftl">
	<#include "/common/upload.ftl">
	<link rel="stylesheet" type="text/css" href="${params.contextPath}/common/ztree/css/zTreeStyle/zTreeStyle.css"/>
	<script type="text/javascript" src="${params.contextPath}/common/ztree/js/jquery.ztree.all.min.js"></script>
	<script type="text/javascript" charset="utf-8" src="${params.contextPath}/common/ueditor/ueditor.config.js"></script>
	<script type="text/javascript" charset="utf-8" src="${params.contextPath}/common/ueditor/ueditor.all.js"></script>
	<script type="text/javascript" charset="utf-8" src="${params.contextPath}/common/ueditor/lang/zh-cn/zh-cn.js"></script>
    <style>
    	body{padding: 10px;padding-top: 0px;}
    	.ui-form{background: #fff;}
    	
    </style>
    <style type="text/css">
    .panel-header{background: #fafafa;}
    .informationTemplate{border: 1px solid #ddd;height: 28px;}
    .informationContainer{padding-top: 10px;}
    </style>
<script type="text/javascript">
$(document).ready(function(){
	//文件上传
		$.upload({uploadSuccess:function(file, data){
			if (!data.success) {
				$.message(data.message);
				return;
			}
		var item = $.parseJSON(data._raw).data[0];
		var imgId = item.id;
		var imgSize = FileUtils.formatSize(file.size);
		var imgUrl = "${params.contextPath}/web/attachment/download/{0}.json".format(imgId);

		var content = '<div style="margin:5px 0px;" class="attachmentDiv">';
			content += '<span><input type="hidden" value="{imageId}" name="fileIds"/></span>';
            content += '<a href="{imageUrl}" target="_blank"><i class="AddFile AddBefImg">{imageName}({imageSize})</i></a>';
			content +='<a onclick="$(this).parent().remove()">&nbsp;<font color="red">删除</font></a></div>';

			content = content.format({imageId:imgId, imageUrl:imgUrl, imageName:item.name, imageSize:imgSize});
			$("#select-file-button").before(content);
		}});
});	
</script>
</head>
<body class="easyui-layout" fit="true" border="false">
<div data-options="region:'east',split:true" title="基本信息" border="false" style="width:450px;">
	<div class="ui-form">
		<form class="layui-form my-form " action="${params.contextPath}/web/instruction/save.json" method="post">
	        <input type="hidden" name="id" value="${params.id}"/>
	        <input type="hidden" name="state" value="1"/>
	        <input type="hidden" name="refid" value="${params.refid}"/>
	        <div class="layui-form-item">
	            <label class="layui-form-label">接收单位<span class="ui-request">*</span></label>
	            <div class="layui-input-block">
	                <input type="hidden" name="actors" id="actors"  class="layui-input">
              			<textarea class="layui-textarea select-tree" style="background: #fafafa;height: 60px;" 
              				readonly="readonly" placeholder="请选择指令接收单位" name="actorNames" 
              				id="actorNames" p="check:true,id:'#actors',name:'#actorNames',url:'${params.contextPath}/web/organization/tree.json'"></textarea> 
	            </div>
	        </div>
	        <div class="layui-form-item">
	            <label class="layui-form-label">指令标题<span class="ui-request">*</span></label>
	            <div class="layui-input-block">
	                <input type="text" class="layui-input" placeholder="必填，最长输入200个字符" name="title" maxlength="50">
	            </div>
	        </div>
	        <div class="layui-form-item">
	            <label class="layui-form-label" >关键字<span class="ui-request">&nbsp;</span></label>
	            <div class="layui-input-block">
	                <input type="text" class="layui-input" placeholder="非必填，多个用“,”分隔" name="keyword" maxlength="500">
	            </div>
	        </div>
	        <div class="layui-form-item" >
	            <label class="layui-form-label">签发人<span class="ui-request">*</span></label>
	            <div class="layui-input-block">
	                <input type="text" class="layui-input" placeholder="必填" name="senderName" maxlength="50" value="${user.name}">
	            </div>
	        </div>
	        <div class="layui-form-item" >
	            <label class="layui-form-label" >签发单位<span class="ui-request">*</span></label>
	            <div class="layui-input-block">
	                <input type="text" class="layui-input" placeholder="必填" name="senderOrgName" maxlength="50" value="${user.organization.name}">
	            </div>
	        </div>
	        <!-- <div class="layui-form-item" >
	            <label class="layui-form-label">指令类型<span class="ui-request">*</span></label>
	            <div class="layui-input-block">
	                <select name="type" onchange="getTemplate(this)">
	                	<option value="">--选择指令模板--</option>
	                	<option value="1">工作指令</option>
	                	<option value="2">情报预警指令</option>
	                	<option value="3">情报采集指令</option>
	                	<option value="4">核查指令</option>
	                	<option value="5">情报通报</option>
	                	<option value="6">其它指令</option>
	                </select>
	            </div>
	        </div>
	        <div class="layui-form-item" style="clear: none;width: 50%;float: left">
	            <label class="layui-form-label">指令模板<span class="ui-request">&nbsp;</span></label>
	            <div class="layui-input-block">
	                <select id="instructionTemplate" onchange="getTemplateContent(this)">
	                	
	                </select>
	            </div>
	        </div>
	         -->
	        
	        <div class="layui-form-item">
		            <label class="layui-form-label">附件</label>
		            <div class="layui-input-block">
		                <a id="select-file-button">添加文件</a>
		            </div>
		        </div>
	        <div class="layui-form-item" style="padding-top: 10px;text-align: center;">
                <input type="button" value="发送指令" class="layui-btn submit_button"/>
            	<!-- <input type="button" value="保存为草稿" class="layui-btn save_button"/> -->
	        </div>
	    </form>
	</div>
</div>
   <div data-options="region:'center',title:'指令正文'" border="false" style="padding: 10px">
   	<div style="line-height: 30px;border-bottom: 1px dashed #ddd;padding-bottom: 10px;">

  		<select id="informationTemplate" class="informationTemplate" onchange="templateChange();">
  			<option value="">---选择指令模板---</option>
  			
  		</select>
  		</div>
       <div id="informationContainer" class="informationContainer">
           <script type="text/plain" id="myEditor" style="width:100%;height:450px;"></script>
       </div>
   </div>
<script>
var opts = {
		//定制工具按钮
		
		initialContent:"录入指令内容...",
		autoClearinitialContent: false,//获取光标是，是否自动清空初始化数据
		elementPathEnabled : false,//是否展示元素路径
		wordCount:false,//是否计数
		autoHeightEnabled:false,//高度是否自动增长
		textarea:"contact_content"//后台接受UEditor的数据的参数名称
    };
<#if (params.id)??>
opts.autoClearinitialContent=false;
<#else>
opts.autoClearinitialContent=true;
</#if>

var ue = null;    

$(function(){
	ue = UE.getEditor('myEditor',opts);
	
	<#if params.id??>
	$.ajax({
        type: "GET",
        url: '${params.contextPath}/web/instruction/query.json',
        data: {id: "${params.id}"},
        success: function(resp){
        	var data = resp.data;
        	for(var key in data){
        		$("[name='" + key + "']").val(data[key]);
        		if(key=='content'){
        			ue.ready(function(){
        				ue.setContent(data['content']);
        			});
        		}
        	}
        }
    });
	</#if>
	
	$(".submit_button").click(function(){
		$("input[name='state']").val("1");
		submitFormFun();
	});
	$(".save_button").click(function(){
		$("input[name='state']").val("0");
		submitFormFun();
	});
	
});

function getTemplate(obj){
	var val = $(obj).val();
	$("#instructionTemplate").empty();
	if(val!=""){
		$.ajax({
	        type: "GET",
	        url: '${params.contextPath}/web/instructionTemplate/list.json',
	        data: {type: val,state:1},
	        success: function(resp){
	        	var data = resp.data;
	        	var option = null;
	        	option = $('<option value="">--选择指令模板--</option>');
	        	$("#instructionTemplate").append(option);
	        	for(var i=0;i<data.length;i++){
	        		var template = data[i];
	        		option = $("<option></option>");
	        		option.val(template.id);
	        		option.text(template.name);
	        		$("#instructionTemplate").append(option);
	        	}
	        }
	    });
	}
}
function getTemplateContent(obj){
	var val = $(obj).val();
	if(val!=""){
		layer.confirm("导入模板后原有指令正文将被覆盖，您确定使用该模板吗?", function(){
			$.ajax({
		        type: "GET",
		        url: '${params.contextPath}/web/instructionTemplate/query.json',
		        data: {id: val},
		        success: function(resp){
		        	var data = resp.data;
		        	var content = data.content;
		        	ue.setContent(content); 
		        	layer.closeAll();
		        }
		    });
		});
		
	}
}
function submitFormFun(){
	var params = {parentRefresh: true, parentClose: true};
    var obj = $(".my-form");
    obj.find("input[type='submit']").prop("disabled", "disabled");
	$.loading();
	obj.ajaxSubmit({
        type: "post",
        url: obj.attr("action"),
        dataType: "json",
        success: function (data) {
            $.closeLoading();
            obj.find("input[type='submit']").removeAttr("disabled");
            
            if (data.success) {
                layer.alert("操作成功", function () {
                    obj.resetForm();
                    try {
                        if (params.parentClose) {
                            parent.layer.closeAll();
                        }
                        if (params.parentRefresh) {
                            parent.DataGrid.refresh();
                        }
                    } catch (e) {
                        if (params.parentClose) {
                            parent.layer.closeAll();
                        }
                    }
                });
                return false;
            }
            layer.msg(data.message);
        }
    });
    return false;
}


</script>
</body>
</html>
